package com.lanou.utp.utils.utilsImpl;

import com.lanou.utp.utils.PasswordManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Component
public class PasswordManagerImpl implements PasswordManager {

    @Value("${com.lanou.password.salt}")
    private String salt;

    @Override
    public String encryptPassword(String password) {

        try {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update((password + salt).getBytes());
            return new BigInteger(1, md.digest()).toString(32);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
    }

}
